var AlumniInfoData = BaseController.extend({
	_data : null,
	_alumusId : null,
	// restful 接口名称
	_restFulServiceName : null,
	// 主键名称
	_primaryKey : null,
	_scopeObject : null,
	_alumniModel : null,
	
	init : function($scope, data, scopeObject, restFulServiceName, primaryKey, alumniModel, alumusId) {
		this._data = data;
		this._scopeObject = scopeObject;
		this._restFulServiceName = restFulServiceName;
		this._alumniModel = alumniModel;
		this._primaryKey = primaryKey;
		this._alumusId = alumusId; 
		this._super($scope);
	}, 
	
	add : function() {
		this.$scope[this._scopeObject] = {};
	},
	
	saveOrUpdate : function(postObject) {
		postObject.alumusId = this._alumusId;
		if (postObject[this._primaryKey]) {
			console.log(postObject);
			this._alumniModel.updateAlumniInfo(postObject, this._primaryKey, this._restFulServiceName, function(data, status, headers, config) {
				this.$scope[this._scopeObject] = false;
			}.bind(this));		
		} else {
			this.create(postObject);
		}
	},
	
	create : function(postObject) {
		this._alumniModel.createAlumniInfo(postObject, this._restFulServiceName, function(data,
				status, headers, config) {
			this._data.push(data);
			this.$scope[this._scopeObject] = false;
		}.bind(this));
	},
	
	edit : function(postObject) {
		this.$scope[this._scopeObject] = postObject;
	}, 
	
	remove : function(index, primaryKeyId) {
		console.log(primaryKeyId);
		this._alumniModel.removeAlumniInfo(this._alumusId, primaryKeyId, this._restFulServiceName, 
				function(data, status, headers, config) {
			this._data.splice(index, 1);
		}.bind(this));
	}
	
});


var AlumniDetailsController = BaseController.extend({
	_alumniModel : null,
	_route : null,
	_location : null,
	
	init : function($scope, alumniModel, route, location) {
		this._alumniModel = alumniModel;
		this._route = route;
		this._location = location;
		this._super($scope)
	},
	
	defineScope : function() {
		var id = this._route.current.params.id;
		this.$scope.alumusId = id;
		this._alumniModel.get(id, function(data, status, headers, config) {
			if (data.personalInfo.male = true) {
				data.personalInfo.male = "MALE";
			} else {
				data.personalInfo.male = "FEMALE";
			}
			console.log(data);
			this.$scope.personalInfo = data.personalInfo;
			this.$scope.association = data.association;
			this.$scope.updatePersonalInfo = this.updatePersonalInfo.bind(this);
			this.$scope.updateAssociation = this.updateAssociation.bind(this);
			this.$scope.educationExperiences = new AlumniInfoData(this.$scope, data.educationExperiences, 
					'educationExperience', 'educationExperience', 
					'educationExperienceId', this._alumniModel, id);
			this.$scope.workExperiences = new AlumniInfoData(this.$scope, data.workExperiences, 
					'workExperience', 'workExperience', 'workExperienceId', this._alumniModel, id);
			this.$scope.donations = new AlumniInfoData(this.$scope, data.donations, 'donation', 
					'donation', 'donationRecordId', this._alumniModel, id);
			this.$scope.activities = new AlumniInfoData(this.$scope, data.activities, 
					'activity', 'activity', 'activityId', this._alumniModel, id);

		}.bind(this));
	},
	
	updatePersonalInfo : function(personalInfo) {
		convertPersonalInfo = {
				alumusId: this.$scope.alumusId,
				name: personalInfo.name, 
				usedName: personalInfo.usedName, 
				identificationType: personalInfo.identificationType, 
				identificationNumber: personalInfo.identificationNumber, 
				locationCountry: personalInfo.countryOfLocation,
				locationtateProvince : personalInfo.stateOfLocation,
				locationCity : personalInfo.cityOfLocation,
				postalAddressCity :　personalInfo.cityOfPostalAddress,
				postalAddressCountry : personalInfo.countryOfPostalAddress,
				postalAddressPostalCode : personalInfo.postalcodeOfPostalAddress,
				postalAddressStateProvince : personalInfo.stateOfPostalAddress,
				postalAddressStreet : personalInfo.cityOfPostalAddress,
				homeTelephone: personalInfo.homeTelephone,
				officeTelephone: personalInfo.officeTelephone,
				email: personalInfo.email,
				cellphone : personalInfo.cellphone,
				wechat: personalInfo.wechat,
				weibo: personalInfo.weibo,
				qq: personalInfo.qq,
				beliving : personalInfo.alive,
				gendar : personalInfo.male,
				politicalStatus : personalInfo.politicalStatus,
				birthday: personalInfo.birthday,
				origin: personalInfo.origin,
				nation: personalInfo.nation
		};
		this._alumniModel.updatePersonalInfo(convertPersonalInfo, function(data, status, headers, config) {
			this._location.path('/alumni/alumni-managment');
		}.bind(this));
	},
	
	updateAssociation : function(association) {
		association.alumusId = this.$scope.alumusId;
		this._alumniModel.updateAssociation(association, function(data, status, headers, config) {
			 this._location.path('/alumni/alumni-managment');
		}.bind(this));	
	}
	
})

AlumniDetailsController.$inject = ['$scope', 'AlumniModel', '$route', '$location'];